package com.txh.blog.util;

import com.aliyun.oss.OSSClient;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.ResourceBundle;

public class OssCliectUtils {

    private static  ResourceBundle bundle = ResourceBundle.getBundle("ossProperties");

    private static String ENDPOINT = bundle.getString("endpoint");

    private static String BUCKETNAME=bundle.getString("bucket");

    private static String ACCESSKEY= bundle.getString("accesskey");

    private static String ACCESSKEYSRECT=bundle.getString("accesskeysercret");

    private static OSSClient ossClient = new OSSClient(ENDPOINT,ACCESSKEY,ACCESSKEYSRECT);

    private OssCliectUtils(){
        throw new AssertionError("This obj Not insatnce");
    }
    public static String uploadByInputStream(InputStream inputStream,String fileName){
        ossClient.putObject(BUCKETNAME,fileName,inputStream);
        return getUrl(fileName);
    }

    public static String getUrl(String fileName){
        //定义过期时间
        Date expire = new Date(System.currentTimeMillis()+3600*1000*24*3650);
        URL url = ossClient.generatePresignedUrl(BUCKETNAME,fileName,expire);
        return url.toString().split("[?]")[0];
    }

    public static void shutdown(){
        ossClient.shutdown();
    }

    public static void deleteFile(String fileName){
        ossClient.deleteObject("wodeimages",fileName);
    }
}
